<template>
  <div>
      <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>欢迎页</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 数据统计 -->
    <!-- Row 组件 提供 gutter 属性来指定每一栏之间的间隔，默认间隔为 0。 -->
    <el-row :gutter="10">
      <el-col :span="6">
        <div class="tongji" style="background: #409efe; border-radius: 4px">
          <div class="title">
            <p>用户统计</p>
            <el-tag size="mini" type="success">实时</el-tag>
          </div>
          <div class="num">{{ usersTotal }}</div>
          <div class="tipper">当前用户数据</div>
        </div>
      </el-col>

      <el-col :span="6">
        <div class="tongji" style="background: #e6a23c; border-radius: 4px">
          <div class="title">
            <p>商品统计</p>
            <el-tag size="mini" type="info">实时</el-tag>
          </div>
          <div class="num">{{ goodsTotal }}</div>
          <div class="tipper">当前总商品数据</div>
        </div>
      </el-col>

      <el-col :span="6">
        <div class="tongji" style="background: #67c23a; border-radius: 4px">
          <div class="title">
            <p>订单统计</p>
            <el-tag size="mini" type="warning">实时</el-tag>
          </div>
          <div class="num">{{ orderTotal }}</div>
          <div class="tipper">当前订单数据</div>
        </div>
      </el-col>

      <el-col :span="6">
        <div class="tongji" style="background: #909389; border-radius: 4px">
          <div class="title">
            <p>销售统计</p>
            <el-tag size="mini" type="primary">实时</el-tag>
          </div>
          <div class="num">{{ totalSalse }}</div>
          <div class="tipper">当前总销售数据</div>
        </div>
      </el-col>
    </el-row>

    <!-- Echarts图片展示结构区域 Echarts为第三方插件库 -->
    <div class="echarts-container">
      <el-card id="echart1"></el-card>
      <el-card id="echart2"></el-card>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
export default {
  name: 'welcome',
  data() {
    return {
      usersTotal: 0,
      goodsTotal: 0,
      orderTotal: 0,
      totalSalse: 0,
      ordersList: [],
    }
  },
  created() {
    this.getUsersList()
    this.getGoodsList()
    this.getOrderList()
  },
  methods: {
    async getUsersList() {
      const { data: res } = await this.$http.get('users', {
        params: {
          pagenum: 1,
          pagesize: 1,
        },
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取用户数据失败！')
      }
      this.usersTotal = res.data.total
      //   console.log(res.data)
    },

    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: {
          pagenum: 1,
          pagesize: 1,
        },
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品数据失败！')
      }
      this.goodsTotal = res.data.total
      //   console.log(res.data)
    },

    async getOrderList() {
      const { data: res } = await this.$http.get('orders')
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单数据失败！')
      }
      this.orderTotal = res.data.total
      this.ordersList = res.data.goods
      //   console.log(res.data)

      //获取总销售额
      res.data.goods.forEach((item) => {
        if (item.pay_status == 1) {
          this.totalSalse += item.order_price
        }
      })
      this.initEchart1()
      this.initEchart2()
    },
    formatTime(time) {
      var date = new Date(time * 1000)
      const year = date.getFullYear()
      const mouth = (date.getMonth() + 1).toString().padStart(2, '0') //padStart(2,'0')取两位不够补0
      const day = date.getDate().toString().padStart(2, '0')
      return `${year}-${mouth}-${day}`
    },
    initEchart1() {
      //创建echarts实例对象
      var chart1 = echarts.init(document.getElementById('echart1'))

      var option = {
        title: {
          text: '订单趋势',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
          },
        },
        xAxis: {
          type: 'category',
          data: [], // ['03.20', '03.21'],
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            type: 'line',
            smooth: true,
            data: [], // [260, 406]
          },
        ],
      }
      // console.log(this.ordersList)
      /*
            对订单数据进行处理
         */
      //先排序
      this.ordersList.sort(function (a, b) {
        return a.create_time - b.create_time
      })
      //按时间把同一天订单金额加在以前
      var orderPriceData = []
      this.ordersList.forEach((item) => {
        if (item.pay_status == 1) {
          var key = this.formatTime(item.create_time)
          if (!orderPriceData[key]) {
            orderPriceData[key] = item.order_price
          } else {
            orderPriceData[key] += item.order_price
          }
        }
      })
      // console.log(orderPriceData)
      //把处理好的数据放到option中
      for (var key in orderPriceData) {
        option.xAxis.data.push(key)
        option.series[0].data.push(orderPriceData[key])
      }
      chart1.setOption(option, true)
    },
    initEchart2() {
      var chart2 = echarts.init(document.getElementById('echart2'))

      var option = {
        title: {
          text: '访客信息',
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)',
        },
        legend: {
          orient: 'vertical',
          left: 'right',
          data: [],
        },
        series: [
          {
            name: '访客信息',
            type: 'pie',
            radius: '55%',
            center: ['50%', '60%'],
            data: [
                {value: 335, name: '直接访问'},
                {value: 310, name: '邮件营销'},
                {value: 234, name: '联盟广告'},
                {value: 135, name: '视频广告'},
                {value: 1548, name: '搜索引擎'}
            ],
            // data: [],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)',
              },
            },
          },
        ],
      }
      chart2.setOption(option,true)
    },
  },
}
</script>

<style lang="less" scoped>
.el-row {
  padding: 0 7px;
  color: white;
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    p {
      margin: 8px 0 8px 10px;
      font-weight: 600;
    }
    .el-tag {
      margin-right: 10px;
    }
  }
  .num {
    text-align: center;
    font-size: 18px;
    font-weight: 700;
  }
  .tipper {
    font-size: 12px;
    margin-left: 10px;
    padding-bottom: 4px;
  }
}
.echarts-container {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  .el-card {
    height: 300px;
    width: 50%;
    padding: 15px;
  }
}
</style>